using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.AssetSystem;
using libsecondlife.InventorySystem;

using Feathertail;

namespace NestBox.Inventory
{
    class display : Feathertail.Command
    {
        FeathertailClient _Client;

        #region Command Members

        public void Setup(FeathertailClient Client)
        {
            _Client = Client;
        }

        public string HandleInput(string[] CmdLine)
        {
            if (CmdLine.Length != 2)
            {
                return GetHelpText();
            }

            StringBuilder output = new StringBuilder();

            // Asset for an item in inventory
            string curDirectory = (string)_Client.FeathertailState[Helpers.CurrentDirectoryKey];
            InventoryFolder iFolder = _Client.Inventory.getFolder(curDirectory);

            iFolder.RequestDownloadContents(false, false, true).RequestComplete.WaitOne(15000, false);
            foreach (InventoryBase ib in iFolder.GetContents())
            {
                if (ib is InventoryItem)
                {
                    InventoryItem item = (InventoryItem)ib;
                    if (item.ItemID.Equals(CmdLine[1]) || item.Name.Equals(CmdLine[1]))
                    {
                        output.AppendLine("Displaying " + item.Name + " [" + item.ItemID + "]");
/*
                        if (item is InventoryNotecard)
                        {
                            InventoryNotecard iNotecard = (InventoryNotecard)item;
                            output.AppendLine("-------------------------------------------");
                            output.AppendLine(iNotecard.Body);
                            output.AppendLine("-------------------------------------------");
                        }
                        else
 */
                        {
                            output.AppendLine(item.Asset.AssetDataToString());
                            output.AppendLine("-------------------------------------------");
                            output.AppendLine(libsecondlife.Helpers.FieldToUTF8String(item.Asset.AssetData));
                        }
                    }
                }
            }

            return output.ToString();
        }

        public string[] GetCommandStrings()
        {
            return new string[] { "display", "view" };
        }

        public string GetSummaryText()
        {
            return "Retrieve item and attempt to display it";
        }

        public string GetHelpText()
        {
            return "Usage: display [itemname | UUID]";
        }

        #endregion
    }
}
